package com.qing.design.patterns.proxy.example;

import com.qing.design.patterns.proxy.example.downloader.YouTubeDownloader;
import com.qing.design.patterns.proxy.example.proxy.YouTubeCacheProxy;
import com.qing.design.patterns.proxy.example.some_cool_media_library.ThirdPartyYouTubeClass;

public class Demo {
	public static void main(String[] args) {
		//下载程序
		YouTubeDownloader naiveDownloader =new YouTubeDownloader(new ThirdPartyYouTubeClass());
		//智能下载程序
		YouTubeDownloader smartDownloader =new YouTubeDownloader(new YouTubeCacheProxy());

		long naive = test(naiveDownloader);
		long smart = test(smartDownloader);
		System.out.print("缓存代理节省的时间： " + (naive - smart) + "ms");
	}

	private static  long test(YouTubeDownloader youTubeDownloader){
		long startTime=System.currentTimeMillis();

		//我们应用程序中的用户行为：
		youTubeDownloader.renderPopularVideos();
		youTubeDownloader.renderVideoPage("catzzzzzzzzz");
		youTubeDownloader.renderPopularVideos();
		youTubeDownloader.renderVideoPage("dancesvideoo");
		// 用户可能经常访问同一个页面。
		youTubeDownloader.renderVideoPage("catzzzzzzzzz");
		youTubeDownloader.renderVideoPage("someothervid");
		long estimatedTime = System.currentTimeMillis() - startTime;
		System.out.print("已用时间: " + estimatedTime + "ms\n");
		return estimatedTime;
	}
}
